﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyMeetup
{
    public class AppSettings
    {
        private IsolatedStorageSettings isolatedStore;


        public AppSettings()
        {
            try
            {
                // Get the settings for this application.
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;

            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (isolatedStore.Contains(Key))
            {
                // If the value has changed
                if (isolatedStore[Key] != value)
                {
                    // Store the new value
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="valueType"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (isolatedStore.Contains(Key))
            {
                value = (valueType)isolatedStore[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            isolatedStore.Save();
        }

       

        public void Logout()
        {
            OAuthToken = "";
            TokenSecret = "";
            OAuthVerifier = "";
            IsAuthenticated = false;
            AuthUser = null;
        }
        
        public string OAuthToken
        {
            get { return GetValueOrDefault<string>("oauthtoken", ""); }
            set
            {
                AddOrUpdateValue("oauthtoken", value);
                Save();
            }
        }

        public string TokenSecret
        {
            get { return GetValueOrDefault<string>("tokensecret", ""); }
            set
            {
                AddOrUpdateValue("tokensecret", value);
                Save();
            }
        }

        public string OAuthVerifier
        {
            get { return GetValueOrDefault<string>("oauthverifier", ""); }
            set
            {
                AddOrUpdateValue("oauthverifier", value);
                Save();
            }
        }

        public bool IsAuthenticated
        {
            get { return GetValueOrDefault<bool>("isauthenticated", false); }
            set
            {
                AddOrUpdateValue("isauthenticated", value);
                Save();
            }
        }

        public Member AuthUser
        {
            get { return GetValueOrDefault<Member>("authuser", null); }
            set
            {
                AddOrUpdateValue("authuser", value);
                Save();
            }
        }


        private const string _firstRunName = "firstRun";
        public bool FirstRun
        {
            get { return GetValueOrDefault(_firstRunName, true); }
            set { if(AddOrUpdateValue(_firstRunName, value)) Save(); }
        }

        private const string _privacyPolicyAcknowledgedName = "privacyPolicyAcknowledged";
        public bool PrivacyPolicyAcknowledged
        {
            get { return GetValueOrDefault(_privacyPolicyAcknowledgedName, false); }
            set { if(AddOrUpdateValue(_privacyPolicyAcknowledgedName, value)) Save(); }
        }

        private const string _useLocServicesName = "useLocServices";
        public bool UseLocServices
        {
            get { return GetValueOrDefault(_useLocServicesName, true); }
            set { if(AddOrUpdateValue(_useLocServicesName, value)) Save(); }
        }


        private const string _zipCodeName = "zipCode";
        public string ZipCode
        {
            get { return GetValueOrDefault(_zipCodeName, string.Empty); }
            set { if(AddOrUpdateValue(_zipCodeName, value)) Save(); }
        }

        private const string _searchRadiusName = "searchRadius";
        public int SearchRadius
        {
            get { return GetValueOrDefault(_searchRadiusName, 10); }
            set { if(AddOrUpdateValue(_searchRadiusName, value)) Save(); }
        }


        public ObservableCollection<Activity> News
        {
            get { return GetValueOrDefault<ObservableCollection<Activity>>("news", new ObservableCollection<Activity>()); }
            set { if(AddOrUpdateValue("news", value)) Save(); }
        }

        public ObservableCollection<IGroup> MyGroups
        {
            get { return GetValueOrDefault<ObservableCollection<IGroup>>("groups", new ObservableCollection<IGroup>()); }
            set { if (AddOrUpdateValue("groups", value)) Save(); }
        }

        public ObservableCollection<Event> UpcomingEvents
        {
            get { return GetValueOrDefault<ObservableCollection<Event>>("events", new ObservableCollection<Event>()); }
            set { if (AddOrUpdateValue("events", value)) Save(); }
        }






    }
}
